package com.snoopy.cloud.controller;

import com.alibaba.fastjson2.JSON;
import com.snoopy.cloud.entity.AccountDTO;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.bulkhead.internal.SemaphoreBulkhead;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author snoopy23
 * @date 2024/6/27
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    String url = "http://localhost:8081/payment/hello";
    @Resource
    private RestTemplate restTemplate;


    @RequestMapping("hello")
    @ResponseBody
    public Object hello(AccountDTO accountDTO){
        Object forObject = restTemplate.getForObject(url, String.class,JSON.toJSON(accountDTO));
        return "hello order："+ JSON.toJSONString(forObject);
    }

    @RequestMapping("testOpenfeign")
    public Object testOpenfeign(AccountDTO accountDTO){
        Object forObject = restTemplate.getForObject(url, String.class,JSON.toJSON(accountDTO));
        return "hello order："+ JSON.toJSONString(forObject);
    }

    @CircuitBreaker(name="joyme-server-payment",fallbackMethod = "hello")
    @RequestMapping("testCircuitBreaker")
    public Object testCircuitBreaker(AccountDTO accountDTO){
        Object forObject = restTemplate.getForObject(url, String.class,JSON.toJSON(accountDTO));
        return "hello order："+ JSON.toJSONString(forObject);
    }
    @Bulkhead(name="joyme-server-payment",fallbackMethod = "hello",type = Bulkhead.Type.SEMAPHORE)
    @RequestMapping("testBulkhead")
    public Object testBulkhead(AccountDTO accountDTO){
        // SemaphoreBulkhead
        // FixedThreadPoolBulkhead
        Object forObject = restTemplate.getForObject(url, String.class,JSON.toJSON(accountDTO));
        return "hello order："+ JSON.toJSONString(forObject);
    }
}
